package Jogo;

import org.newdawn.slick.AppGameContainer;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.fills.GradientFill;
import org.newdawn.slick.geom.Rectangle;

import Player.Player;

public class Escape extends BasicGame {

	public Escape() {
		super("Escape!");
	}

	Player player = null;
	Fundo fundo = null;
	Pedra[] pedras = new Pedra[5];
	float rectX = 500;
	float rectY = 1;
	int health = 100;
	int maxHealth = 100;
	


	Camera camera = null;

	public void render(GameContainer container, Graphics g)
			throws SlickException {

		g.translate(((container.getWidth() / 2) - this.camera.getCameraX()),
				((container.getHeight() / 2) - this.camera.getCameraY()));

		fundo.draw(0, -4500);

		g.drawAnimation(player, player.getPlayerX(), player.getPlayerY());

		for (int i = 0; i < pedras.length; i++){
			g.drawAnimation(pedras[i], pedras[i].getX(), pedras[i].getY());
		}

		g.resetTransform();
		
		
		
		Rectangle bar = new Rectangle(rectX, rectY, 300 * health / maxHealth, 20);
        GradientFill fill = new GradientFill(rectX, 0, new Color(255, 60, 0),
        		rectX + 300, 0, new Color(255, 180, 0));

        g.setColor(Color.darkGray);
        g.fillRect(rectX, rectY, 300, 20);
        g.fill(bar, fill); 
    }
		

	

	public void init(GameContainer container) throws SlickException {
		SpriteSheet sheet = new SpriteSheet("Imagens/robo.png", 32, 32);
		SpriteSheet sheet1;
		fundo = new Fundo("Imagens/back2test.png", 0, 0);

		player = new Player(container, 400, 768);
		camera = new Camera(400, 400);
		container.setVSync(true);
		player.setAutoUpdate(true);

		for (int i = 0; i < pedras.length; i++){
			pedras[i] = new Pedra((float) Math.random()  * (800 - 180), -500);


			for (int frame1 = 0; frame1 < 1; frame1++) {

				double sorteio = Math.random();

				if (sorteio < 0.33) {
					System.out.println("1");
					sheet1 = new SpriteSheet("Imagens/pedra1.png", 180, 180);
				} else if (sorteio < 0.66) {
					System.out.println("2");
					sheet1 = new SpriteSheet("Imagens/pedra2.png", 180, 180);
				} else {
					System.out.println("3");
					sheet1 = new SpriteSheet("Imagens/pedra3.png", 180, 180);
				}


				pedras[i].addFrame(sheet1.getSprite(frame1, 0), 200);
			}
		}

		for (int frame = 0; frame < 3; frame++) {
			player.addFrame(sheet.getSprite(frame, 0), 200);

		}

	}

	public void update(GameContainer container, int delta)
			throws SlickException {
		


		for (int i = 0 ; i < pedras.length; i++){	

			pedras[i].updatePedra();

			if (pedras[i].getY() > camera.getCameraY() + 400) {
				pedras[i].setY(camera.getCameraY() - 600);
				pedras[i].setX((float) Math.random()  * (800 - 180));
			}

		}		

		if (player.getPlayerY() < 400) {

			this.camera.setCameraY(player.getPlayerY());

		}

		player.updatePlayer();

		if (this.camera.getCameraY() < -4168) {
			this.camera.setCameraY(-4168);
		}

		if (player.getPlayerY() < -4276) { // Ajustar de acordo com a arte final
			player.setPlayerY(-4276);
		}

		if (player.getPlayerX() < 0) {
			player.setPlayerX(0);

		}

		if (player.getPlayerX() > 768) {
			player.setPlayerX(768);
		}

		if (player.getPlayerY() > 700 - 32) {
			player.setPlayerY(700 - 32);
		}

	}

	public static void main(String[] args) throws SlickException {
		AppGameContainer app = new AppGameContainer(new Escape());

		app.setDisplayMode(800, 600, false);
		app.start();
	}
}
